package vienan.app.expandableswipelistview.test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ditclear.swipelayout.SwipeDragLayout;

import java.util.ArrayList;
import java.util.List;

import vienan.app.expandableswipelistview.R;

public class StaggeredHomeAdapter extends RecyclerView.Adapter<StaggeredHomeAdapter.MyViewHolder> {

    private List<String> mDatas;
    private Context mContext;
    private List<SwipeDragLayout> openList = new ArrayList<>();

    public StaggeredHomeAdapter(Context context, List<String> datas) {
        mContext = context;
        mDatas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.childTitle.setText(mDatas.get(position));

        /*holder.info.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
                //holder.swipeLayout.smoothClose(true);
            }
        });

        holder.info.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                removeData(pos);
                return false;
            }
        });*/

        holder.iv_star.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "click " + position + " star", Toast.LENGTH_SHORT).show();
            }
        });
        holder.iv_trash.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "click " + position + " trash", Toast.LENGTH_SHORT).show();
            }
        });

        holder.swipeLayout.addListener(new SwipeDragLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeDragLayout layout) {

            }

            @Override
            public void onStartClose(SwipeDragLayout layout) {

            }

            @Override
            public void onUpdate(SwipeDragLayout layout, float offset) {

            }

            @Override
            public void onOpened(SwipeDragLayout layout) {
                for (SwipeDragLayout openView : openList) {
                    openView.smoothClose(true);
                }
                openList.clear();
                openList.add(holder.swipeLayout);
            }

            @Override
            public void onClosed(SwipeDragLayout layout) {

            }

            @Override
            public void onCancel(SwipeDragLayout layout) {

            }

            @Override
            public void onClick(SwipeDragLayout layout) {
                Toast.makeText(mContext, "position click:" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClick(SwipeDragLayout layout) {
                Toast.makeText(mContext, "position long:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void removeData(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }

    class MyViewHolder extends ViewHolder {

        SwipeDragLayout swipeLayout;
        LinearLayout info;
        LinearLayout infoTwo;

        ImageView iv_star;
        ImageView iv_trash;
        TextView childTitle;


        public MyViewHolder(View view) {
            super(view);
            swipeLayout = (SwipeDragLayout) view.findViewById(R.id.sample);
            info = (LinearLayout) view.findViewById(R.id.item_surface);
            infoTwo = (LinearLayout) view.findViewById(R.id.info_two);
            childTitle = (TextView) view.findViewById(R.id.tv_title);
            iv_star = (ImageView) view.findViewById(R.id.star);
            iv_trash = (ImageView) view.findViewById(R.id.trash);
        }

    }

    private OnItemClickLitener mOnItemClickLitener;

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
